using UnityEngine;
using System.Collections;

public class Sprites {
	
	private int uvAnimationTileX = 0;
	private int framesPerSecond = 0;
	private bool freeze = false;
	private int startFrame = 0;
	private int endFrame = 0;
	private Renderer renderer;
	private Vector2 tileSize;
	private float offsety;
	public bool active = false;
	public float index;
	
	private float time = 0;
	public Vector3[] innatePosition;
	
	public Sprites(Renderer _renderer, Vector2 _tileSize,float starty, float initialFrame)
	{
		renderer = _renderer;
		tileSize = new Vector2 (_tileSize.x / renderer.material.mainTexture.width,
								 _tileSize.y / renderer.material.mainTexture.height);
		
		uvAnimationTileX = renderer.material.mainTexture.width / (int)_tileSize.x;
		
		offsety = starty / renderer.material.mainTexture.height;
		
		renderer.material.SetTextureScale ("_MainTex", tileSize);
		
		index = initialFrame;
		
		int uIndex = (int)index % uvAnimationTileX;
	    int vIndex = (int)index / uvAnimationTileX;
	
	    // build offset
	    // v coordinate is the bottom of the image in opengl so we need to invert.
	    Vector2 offset = new Vector2 (uIndex * tileSize.x, 1.0f - offsety - tileSize.y - vIndex * tileSize.y);
		
	    renderer.material.SetTextureOffset ("_MainTex", offset);
	
		int tileAmount = (int)(renderer.material.mainTexture.width / _tileSize.x) * (int)(renderer.material.mainTexture.height / _tileSize.y);
		innatePosition = new Vector3[tileAmount];
	}

	
	// Update is called once per frame
	public void Update () {
		if(active)
		{
			time += Time.deltaTime;
			
			// Calculate index
			index = (int)(time * framesPerSecond);
			if(index > endFrame)
			{
				time = (float)startFrame/(float)framesPerSecond;
				index = endFrame;
				if(freeze)
				{
					active = false;
					index = endFrame;
				}
			}
			else if(index < startFrame)
			{
				index = startFrame;
				time = (float)startFrame/(float)framesPerSecond;
			}
		   //Debug.Log(index);
		    // split into horizontal and vertical index
		    int uIndex = (int)index % uvAnimationTileX;
		    int vIndex = (int)index / uvAnimationTileX;
		
		    // build offset
		    // v coordinate is the bottom of the image in opengl so we need to invert.
		    Vector2 offset = new Vector2 (uIndex * tileSize.x, 1.0f - offsety - tileSize.y - vIndex * tileSize.y);
			//Debug.Log(index);
		    renderer.material.SetTextureOffset ("_MainTex", offset);
		}
	}
	
	public void Play(int fps)
	{
		framesPerSecond = fps;
		active = true;
	}
	
	public int getCurrentFrameNumber()
	{
		return (int)index - startFrame;
	}
	
	public Vector3 getInnatePosition()
	{
		return innatePosition[(int)index];
	}
	
	public void Play(SpriteState currentState)
	{
		//Debug.Log(currentState._from);
		active = true;
		framesPerSecond = currentState._fps;
		startFrame = currentState._from;
		endFrame = currentState._to;
		freeze = currentState._isfreeze;
		time = 0;
	}
	
	public void playOneShot(SpriteState currentState)
	{
		active = true;
		framesPerSecond = currentState._fps;
		startFrame = currentState._from;
		endFrame = currentState._to;
		freeze = currentState._isfreeze;
		time = 0;
	}
}
